# -*- coding: utf-8 -*-

import os
from django.core.management.base import BaseCommand


class Command(BaseCommand):
    """
    清理迁移历史命令：python manage.py clearmigrations
    """

    def add_arguments(self, parser):
        pass

    def handle(self, *args, **options):
        exclude = ["venv", 'env']  # 需要排除的文件目录
        for root, dirs, files in os.walk('.'):
            dirs[:] = list(set(dirs) - set(exclude))
            if 'migrations' in dirs:
                dir = dirs[dirs.index('migrations')]
                print(f"Clear Migrations for model \'{root}\':")
                for root_j, dirs_j, files_j in os.walk(os.path.join(root, dir)):
                    for file_k in files_j:
                        if file_k != '__init__.py':
                            dst_file = os.path.join(root_j, file_k)
                            print('    - Delete file', dst_file)
                            os.remove(dst_file)
                print('    - Model Migrations Is Clean!')
